﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebSocketChat.Web.Models;

namespace WebSocketChat.Web.Controllers
{
    public class ChatUsersController : Controller
    {
        private WebSocketChatEntities db = new WebSocketChatEntities();

        // GET: ChatUsers
        public ActionResult Index()
        {
            var chatUser = db.ChatUser.ToList();
            return View(chatUser);
        }

        // GET: ChatUsers/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChatUser chatUser = db.ChatUser.Find(id);
            if (chatUser == null)
            {
                return HttpNotFound();
            }
            return View(chatUser);
        }

        // GET: ChatUsers/Create
        public ActionResult Create()
        {
            ViewBag.FriendshipPolicyID = new SelectList(db.FriendshipPolicy, "ID", "FriendshipPolicy1");
            ViewBag.UserStateID = new SelectList(db.UserState, "ID", "StateName");
            return View();
        }

        // POST: ChatUsers/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,LoginID,NickName,PassWords,SignaTures,Sex,Birthday,Telephone,Name,Email,Intro,HeadPortrait,ShengXiao,Age,Constellation,BloodType,SchoolTag,Vocation,NationID,ProvinceID,CityID,FriendshipPolicyID,UserStateID,FriendPolicyQuestion,FriendPolicyAnswer,FriendPolicyPassword")] ChatUser chatUser)
        {
            if (ModelState.IsValid)
            {
                db.ChatUser.Add(chatUser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.FriendshipPolicyID = new SelectList(db.FriendshipPolicy, "ID", "FriendshipPolicy1", chatUser.FriendshipPolicyID);
            ViewBag.UserStateID = new SelectList(db.UserState, "ID", "StateName", chatUser.UserStateID);
            return View(chatUser);
        }

        // GET: ChatUsers/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChatUser chatUser = db.ChatUser.Find(id);
            if (chatUser == null)
            {
                return HttpNotFound();
            }
            ViewBag.FriendshipPolicyID = new SelectList(db.FriendshipPolicy, "ID", "FriendshipPolicy1", chatUser.FriendshipPolicyID);
            ViewBag.UserStateID = new SelectList(db.UserState, "ID", "StateName", chatUser.UserStateID);
            return View(chatUser);
        }

        // POST: ChatUsers/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,LoginID,NickName,PassWords,SignaTures,Sex,Birthday,Telephone,Name,Email,Intro,HeadPortrait,ShengXiao,Age,Constellation,BloodType,SchoolTag,Vocation,NationID,ProvinceID,CityID,FriendshipPolicyID,UserStateID,FriendPolicyQuestion,FriendPolicyAnswer,FriendPolicyPassword")] ChatUser chatUser)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chatUser).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FriendshipPolicyID = new SelectList(db.FriendshipPolicy, "ID", "FriendshipPolicy1", chatUser.FriendshipPolicyID);
            ViewBag.UserStateID = new SelectList(db.UserState, "ID", "StateName", chatUser.UserStateID);
            return View(chatUser);
        }

        // GET: ChatUsers/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChatUser chatUser = db.ChatUser.Find(id);
            if (chatUser == null)
            {
                return HttpNotFound();
            }
            return View(chatUser);
        }

        // POST: ChatUsers/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ChatUser chatUser = db.ChatUser.Find(id);
            db.ChatUser.Remove(chatUser);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
